//
//  LCCountdownView.m
//  计时器
//
//  Created by admin on 17/1/2.
//  Copyright © 2017年 DDHW. All rights reserved.
//

#import "LCCountdownView.h"

@interface LCCountdownView ()

@property (nonatomic) NSTimer* timer;
@property (nonatomic) UILabel* countdownLabel;
@property (nonatomic) UIImageView* countdownImg;

@property (nonatomic) int currentCountdownValue;

@end
@implementation LCCountdownView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = frame;
        [self updateAppearance];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aCoder {
    if(self = [super initWithCoder:aCoder]){
        [self updateAppearance];
    }
    return self;
}

- (void) updateAppearance
{
    [[self subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    self.opaque = NO;
    self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:self.backgroundAlpha];

    
    self.countdownImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 283, 111)];
    self.countdownImg.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview: self.countdownImg];
    [self.countdownImg setCenter:CGPointMake(self.width * 0.5, self.height * 0.5)];
}



- (void) layoutSubviews
{
    [super layoutSubviews];
    
//    self.countdownLabel.frame = CGRectMake(0, 0, 30, 30);
//    [self.countdownLabel setCenter:self.center];
}

#pragma mark - start/stopping
- (void) start
{
    [self stop];
    self.currentCountdownValue = self.countdownFrom;
    
//    self.countdownLabel.text = [NSString stringWithFormat:@"%d", self.countdownFrom];
    
    self.countdownImg.image = [UIImage imageNamed:@"ready"];
    CGAffineTransform transform = CGAffineTransformMakeScale(0.2, 0.2);
    self.countdownImg.transform = transform;
    
    [self animate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                  target:self
                                                selector:@selector(animate)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void) stop
{
    if (self.timer && [self.timer isValid]) {
        [self.timer invalidate];
    }
}

#pragma mark - animation stuff

- (void) animate
{
    [UIView animateWithDuration:0.9 animations:^{
        
        self.countdownImg.transform = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        if (finished) {
            
            if (self.currentCountdownValue == 0) {
                [self stop];
                if (self.delegate) {
                    [self.delegate countdownFinished:self];
                    [self removeFromSuperview];
                }
                
            } else {
                
                self.countdownImg.transform = CGAffineTransformMakeScale(0.2, 0.2);
                --self.currentCountdownValue;
                if(self.currentCountdownValue == 2)
                {
                    [self.countdownImg setImage:[UIImage imageNamed:@"ready"]];
                }if(self.currentCountdownValue == 1)
                {
                    [self.countdownImg setImage:[UIImage imageNamed:@"ready"]];
                }
                
                if (self.currentCountdownValue == 0) {
                    [self.countdownImg setImage:[UIImage imageNamed:@"go"]];
                }
            }
        }
    }];
}

#pragma mark - custom getters
- (NSString*)finishText
{
    if (!_finishText) {
        _finishText = @"Go";
    }
    
    return _finishText;
}

- (float)backgroundAlpha
{
    if (_backgroundAlpha == 0) {
        _backgroundAlpha = 0;
    }
    
    return _backgroundAlpha;
}

- (int) countdownFrom
{
    if (_countdownFrom == 0) {
        _countdownFrom = kDefaultCountdownFrom;
    }
    
    return _countdownFrom;
}

- (UIColor*)countdownColor
{
    if (!_countdownColor) {
        _countdownColor = [UIColor whiteColor];
    }
    
    return _countdownColor;
}

- (NSString *)fontName
{
    if (!_fontName) {
        _fontName = @"HelveticaNeue-Medium";
    }
    
    return _fontName;
}




@end
